<?php

/**
 *
 * Add this form to user edit page
 */
add_action( 'show_user_profile', 'accs_user_subscribe_form' );
add_action( 'edit_user_profile', 'accs_user_subscribe_form' );

/**
 *
 * Function for display what the user is subscribed to
 */
 if (!function_exists("accs_user_subscribe_form")){
	function accs_user_subscribe_form( $user ) {
	
	    $title = get_option(ACCS_PREFIX . 'USER_PROFILE_TITLE_PREFIX');
	
	    //Load User's Subscriptions
	    $subscriptions = array();
	    $usermeta = get_user_meta($user->ID, ACCS_SUBSCRIPTION_KEY, false);
	    if (isset($usermeta[0]))
	        $subscriptions = $usermeta[0];
	
	    //Load possible subscriptions (categories)
	    $categories = get_categories( array(
	        'orderby'=>'name', 
	        'order'=>'ASC',
	        'hide_empty' => 0));
	
	    $required_categories = get_option(ACCS_PREFIX . 'REQUIRED_SUBSCRIPTION_CATEGORIES');
	
	    //start form
	    echo '<h3>' . $title . ' - Cateogory Subscriptions</h3>';
	
	    //DISPLAY REQUIRED SUBSCRIPTION CATEGORIES 
	    foreach($required_categories as $cat) {
	        echo '<input type="checkbox" name="'. ACCS_SUBSCRIPTION_KEY .'[]" value="' . $cat;
	        echo '" disabled="disabled" checked="checked">' . $cat . '</input><br />';
	    }
	
	    //DISPLAY USER CONFIGURABLE SUBSCRIPTION CATEGORIES
	    foreach ($categories as $cat){ 
	        //If category is in array of required categories or Uncategorized, then skip it
	        if(in_array($cat->name, $required_categories) || $cat->name == 'Uncategorized') 
	            continue;
	
	       	echo '<input type="checkbox" name="'. ACCS_SUBSCRIPTION_KEY .'[]" value="';
	        echo $cat->name . '" ';
	        //mark the box checked if it is in user's subscriptions
	        if(!empty($subscriptions))
	        {
		        foreach ( $subscriptions as $sub ) 
		        {            
		            if($cat->cat_name == esc_attr($sub)){               
		                echo ' checked="checked"';
		                break; //break out of loop
		            } 
		        }      
	         }       
	        	echo '>' . $cat->name . '</input><br />'; // close html input form
	      } 
	}
}
/**
 *
 * Display Audiences the User is in 
 */
add_action( 'show_user_profile', 'accs_user_audience' );

/**
 *
 * Function for display what the user is subscribed to
 */
  if (!function_exists("accs_user_audience")){
	function accs_user_audience( $user ) {
	
	    $title = get_option(ACCS_PREFIX . 'USER_PROFILE_TITLE_PREFIX');
	
	    //Load User's Audiences
	    $audience = array();
	    $usermeta = get_user_meta($user->ID, ACCS_AUDIENCE_KEY, false);
	    if (isset($usermeta[0]))
	        $audience = $usermeta[0];
	
	    //TODO: CLEAN THIS UP AND MAKE IT PRETTY
	    echo '<h3>' . $title . ' - Audiences</h3>';
	   	echo '<b>' . $audience[0] .'</b><br />';
	    
	}
}
/**
 *
 * Run the accs_user_subscribe_update function when the following
 * two actions are fired.
 */
add_action( 'personal_options_update', 'accs_user_subscribe_update' );
add_action( 'edit_user_profile_update', 'accs_user_subscribe_update' );

/**
 *
 * Update the user with the category subscription information
 */
if (!function_exists("accs_user_subscribe_update")){
	function accs_user_subscribe_update( $user_id ) {
	
	    /* Check if the current user has permission to edit the user. */
	    if ( !current_user_can( 'edit_user', $user_id ) )
	        return false;
	
	    //THIS WILL ONLY UPDATE USER CONFIGURABLE SUBSCRIPTIONS
	    $user_subs = $_POST[ACCS_SUBSCRIPTION_KEY];
		if(empty($user_subs))
			$user_subs = array();
	
	    /* Update the user's subscriptions */
	    update_user_meta( $user_id, ACCS_SUBSCRIPTION_KEY, $user_subs, false ); 
	}
}
?>